package com.diandi.signature.utils;



import com.diandi.signature.enums.fdd.CheckParamsType;
import com.diandi.signature.enums.fdd.ReturnCode;
import com.diandi.signature.sign.Tools;
import com.diandi.signature.url.RegexUtils;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 校验业务端入参
 * Created by Administrator on 2017/6/29.
 */
public class CheckParams {
    /**
     * 校验业务平台入参
     *
     * @param checkList 根据参数校验类型来判断
     * @return
     * @throws Exception
     */
    public static Map<String, String> checkParams(List<Object[]> checkList){
        Map<String, String> returnMap = new HashMap<String, String>();
        if (null != checkList && !checkList.isEmpty()) {
            for (Object[] Objects : checkList) {
                String key = Objects[0].toString();
                String value = Objects[1].toString();
                String checkCode = Objects[2].toString();
                if (CheckParamsType.金额.getType().equals(checkCode)) {
                    if (!RegexUtils.checkDecimals(value)) {
                        returnMap = Tools.createReturnParams(ReturnCode.err_1003, ReturnCode.err_1003.getMsg(), key);
                        return returnMap;
                    }
                }
                if (CheckParamsType.身份证号.getType().equals(checkCode)) {
                    if (!RegexUtils.checkIdCard(value)) {
                        returnMap = Tools.createReturnParams(ReturnCode.err_1003, ReturnCode.err_1003.getMsg(), key);
                        return returnMap;
                    }
                }
                if (CheckParamsType.枚举校验.getType().equals(checkCode)) {
                    try{
                        Class clazz = Class.forName(Objects[3].toString());
                        Method method = clazz.getMethod("getEmun", String.class);
                        if (null == method.invoke(null, value)) {
                            returnMap = Tools.createReturnParams(ReturnCode.err_1003, ReturnCode.err_1003.getMsg(), key);
                            return returnMap;
                        }
                    }catch (Exception e){
                        returnMap = Tools.createReturnParams(ReturnCode.err_9002, ReturnCode.err_9002.getMsg(), key);
                        return returnMap;
                    }
                }
            }
        }
        return returnMap;
    }

    /**
     * 判断业务平台必输入参是否存在
     *
     * @param params  参数集合
     * @param strings 需要校验的key
     * @return
     */
    public static Map<String, String> checkParamsIsExist(Map<String, String> params, String... strings) {
        Map<String, String> returnMap = new HashMap<String, String>();
        for (String string : strings) {
            if (!params.containsKey(string) || params.get(string).isEmpty()) {
                returnMap = Tools.createReturnParams(ReturnCode.err_1000, ReturnCode.err_1000.getMsg(), string);
                return returnMap;
            }
        }
        return returnMap;
    }

    public static Map<String, String> checkCAParamsIsExist(Map<String, String> params, String... strings) {
        Map<String, String> returnMap = new HashMap<String, String>();
        for (String string : strings) {
            if (!params.containsKey(string) || params.get(string).isEmpty()) {
                returnMap = Tools.createCAReturnParams(ReturnCode.err_1000, ReturnCode.err_1000.getMsg(), string);
                return returnMap;
            }
        }
        return returnMap;
    }
}
